import {Component} from '@angular/core';
import {AlertController, IonicPage, NavController} from 'ionic-angular';
import {CustomerProvider} from "../../../providers/customerProvider";
import {HomePage} from "../home/home";
import {Http} from "@angular/http";
import {CustomerLoginPage} from '../customer-login/customer-login';

/**
 * Generated class for the UserLoginPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-customer-register',
  templateUrl: 'customer-register.html',
})
export class CustomerRegisterPage {

  customerLoginId: string;
  customerPassword: string;
  confirmedPassword: string;
  customerEmail: string;

  constructor(public navCtrl: NavController,
              public alert: AlertController,
              public auth: CustomerProvider,
              public http: Http) {
  }

  register() {
    if (
      this.customerLoginId == null || this.customerLoginId == '' ||
      this.customerPassword == null || this.customerPassword == '' ||
      this.confirmedPassword == null || this.confirmedPassword == '' ||
      this.customerEmail == null || this.customerEmail == ''
    ) {
      this.alertMsg('Error', 'Username, Password Or Email can\'t be null!');
      return false;
    }

    let data = {
      Customer_Login_ID: this.customerLoginId,
      Customer_Password: this.customerPassword,
      Customer_Password_confirmation: this.confirmedPassword,
      Customer_Email: this.customerEmail
    };
    this.auth.register(data).subscribe(res => {
      if (res.success) {
        this.alertMsg('Success', res.message);
        this.navCtrl.setRoot(HomePage);

      }
      else
        this.alertMsg('error', res.message);
    });
  }

  private alertMsg(title: string, subTitle: string) {
    let alert = this.alert.create({
      title: title,
      subTitle: subTitle,
      buttons: ['OK']
    });
    alert.present();
  }

  back() {
    this.navCtrl.setRoot(CustomerLoginPage);
  }


}
